package com.hxzy.controller.system;

import com.hxzy.common.core.controller.BaseController;
import com.hxzy.common.core.domain.AjaxResult;
import com.hxzy.common.core.domain.R;
import com.hxzy.common.core.domain.TreeSelect;
import com.hxzy.common.core.domain.entity.CmsCategory;
import com.hxzy.common.core.domain.entity.SysDept;
import com.hxzy.common.core.domain.entity.SysDictType;
import com.hxzy.common.core.domain.entity.SysMenu;
import com.hxzy.common.enums.AckCode;
import com.hxzy.system.domain.dto.CategoryListDto;
import com.hxzy.system.domain.dto.SysMenuSearchDTO;
import com.hxzy.system.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Objects;

/**
 * @author 64689
 */

@RestController
@RequestMapping(value = "/api")
@Api(tags = "栏目")
public class CategoryController extends BaseController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 栏目全查
     * @return
     */

    @GetMapping(value = "/system/category/list")
    public R selectAll (CategoryListDto category){
        List<CmsCategory> list=this.categoryService.selectMenuList(category);
        return R.okHasData(list);
    }

    /**
     * 栏目标题
     * @return
     */

    @GetMapping(value = "/system/category/selectByTitle/{categoryTitle}")
    public R selectByTitle (@PathVariable @RequestBody String categoryTitle){
        List<CmsCategory> list = this.categoryService.selectBy(categoryTitle);
        return R.okHasData(list);
    }

    @ApiOperation(value = "加载树型栏目数据")
    @GetMapping(value = "/system/category/treeselect")
    public R menuTreeSelect(){
        //根据条件查询菜单，这里没有给对象赋值，相当于全查询
        List<CmsCategory> menus = categoryService.selectMenuList(new CategoryListDto());
        //把SysMenu转换成树型节点
        List<TreeSelect> treeSelects=categoryService.buildMenuTreeSelect(menus);
        return R.okHasData(treeSelects);
    }

    @ApiOperation(value = "新增栏目")
    @PostMapping(value = "/system/category/add")
    public R add(@RequestBody @Valid CmsCategory category){
//         CmsCategory cmsCategory=categoryService.selectByTitle(category.getCategoryTitle());
//         if(Objects.isNull(cmsCategory)){
//             return R.build(AckCode.IS_EXISTS);
//         }
        boolean save = categoryService.save(category);
         if(!save){
             return R.okHasData("新增失败");
         }
         return  R.ok();
    }

    @ApiOperation(value = "根据id查询部门信息")
    @GetMapping(value = "/system/category/{id}")
    public R getDeptById (@PathVariable Long id) {
        CmsCategory category = categoryService.selectDeptById(id);
        if (Objects.isNull(category)) {
            return R.build(AckCode.NOT_FOUND_DATA);
        }
        return R.okHasData(category);
    }



    /**
     * 获取栏目选择框列表
     */
    @GetMapping("/system/category/select")
    public R optionselect()
    {
        List<SysDictType> dictTypes = categoryService.selectTitleAll();
        return R.okHasData(dictTypes);
    }


    @ApiOperation(value = "新增栏目")
    @PostMapping(value = "/system/category/update")
    public R update(@RequestBody @Valid CmsCategory category){
//         CmsCategory cmsCategory=categoryService.selectByTitle(category.getCategoryTitle());
//         if(Objects.isNull(cmsCategory)){
//             return R.build(AckCode.IS_EXISTS);
//         }
        boolean save = categoryService.updateBy(category);
        if(!save){
            return R.okHasData("修改失败");
        }
        return  R.ok();
    }

    @DeleteMapping(value = "/system/category/remove/{id}")
    @ApiOperation(value = "根据id删除栏目信息")
    public R remove (@PathVariable @RequestBody Long id) {
        boolean boo = categoryService.hasChildById(id);
        if (boo) {
            return R.build(AckCode.NOT_DELETE);
        }
        boolean b = categoryService.removeById(id);
        if(!b){
            return R.okHasData("删除失败");
        }
        return R.ok();

    }



}
